package org.gcb.core.windows;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Logger;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import org.gcb.core.model.Ability;
import org.gcb.core.model.AbilityType;
import org.gcb.core.model.Toon;
import org.gcb.core.ui.Utilities;
import org.gcb.core.ui.model.ExperienceProgressModel;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Property;
import org.jdesktop.beansbinding.PropertyStateEvent;
import org.jdesktop.beansbinding.PropertyStateListener;
import org.openide.util.LookupEvent;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.LookupListener;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.gcb.core.windows//ToonSandbox//EN",
autostore = false)
public final class ToonSandboxTopComponent extends TopComponent implements LookupListener{

    private Lookup.Result<Toon> result = null;
    /** path to the icon used by the component and its open action */
    private static ToonSandboxTopComponent instance;
    static final String ICON_PATH = "org/gcb/core/windows/status_online.png";
    private static final String PREFERRED_ID = "ToonSandboxTopComponent";

    private PropertyChangeListener pcl = new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };

    public ToonSandboxTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(ToonSandboxTopComponent.class, "CTL_ToonSandboxTopComponent"));
        setToolTipText(NbBundle.getMessage(ToonSandboxTopComponent.class, "HINT_ToonSandboxTopComponent"));
        setIcon(ImageUtilities.loadImage(ICON_PATH, true));

    }

    private void updateAbilityScore(Integer amount, Ability ability, JSpinner spinner){
        Integer before = ability.getScore();
        Integer after = before + amount;
        ability.setScore(after);
        spinner.setValue(after);
        jLabel8.setText("Before: "+before+" | After: "+after);
        LookupEvent event = new LookupEvent(WindowManager.getDefault().findTopComponent("ToonListingTopComponent").getLookup().lookupResult(Toon.class));
        Utilities.updateUI(event, SkillsTopComponent.findInstance(), DefensesTopComponent.findInstance());
    }

    private void updateAbilityMisc(Integer amount, Ability ability, JSpinner spinner){
        Integer before = ability.getMisc();
        Integer after = before + amount;
        ability.setMisc(after);
        spinner.setValue(after);
        jLabel8.setText("Before: "+before+" | After: "+after);
    }

    private void updateModifier(Ability ability, JTextField txtField) {
        txtField.setText(ability.getModifier()+"");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        lblStr = new javax.swing.JLabel();
        spnStrScore = new javax.swing.JSpinner();
        spnStrBonus = new javax.swing.JSpinner();
        txtStrModifier = new javax.swing.JTextField();
        lblCon = new javax.swing.JLabel();
        spnConScore = new javax.swing.JSpinner();
        spnConBonus = new javax.swing.JSpinner();
        txtConModifier = new javax.swing.JTextField();
        lblDex = new javax.swing.JLabel();
        spnDexScore = new javax.swing.JSpinner();
        spnDexBonus = new javax.swing.JSpinner();
        txtDexModifier = new javax.swing.JTextField();
        lblInt = new javax.swing.JLabel();
        spnIntScore = new javax.swing.JSpinner();
        spnIntBonus = new javax.swing.JSpinner();
        txtIntModifier = new javax.swing.JTextField();
        lblWis = new javax.swing.JLabel();
        spnWisScore = new javax.swing.JSpinner();
        spnWisBonus = new javax.swing.JSpinner();
        txtWisModifier = new javax.swing.JTextField();
        lblCha = new javax.swing.JLabel();
        spnChaScore = new javax.swing.JSpinner();
        spnChaBonus = new javax.swing.JSpinner();
        txtChaModifier = new javax.swing.JTextField();
        prgsExperience = new javax.swing.JProgressBar();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.jPanel1.border.title"), javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP)); // NOI18N
        jPanel1.setLayout(new java.awt.GridBagLayout());

        org.openide.awt.Mnemonics.setLocalizedText(lblStr, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.lblStr.text")); // NOI18N
        jPanel1.add(lblStr, new java.awt.GridBagConstraints());

        spnStrScore.setMinimumSize(new java.awt.Dimension(40, 20));
        spnStrScore.setPreferredSize(new java.awt.Dimension(40, 20));
        spnStrScore.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnStrScoreMouseWheelMoved(evt);
            }
        });
        jPanel1.add(spnStrScore, new java.awt.GridBagConstraints());

        spnStrBonus.setMinimumSize(new java.awt.Dimension(40, 20));
        spnStrBonus.setPreferredSize(new java.awt.Dimension(40, 20));
        spnStrBonus.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnStrBonusMouseWheelMoved(evt);
            }
        });
        jPanel1.add(spnStrBonus, new java.awt.GridBagConstraints());

        txtStrModifier.setEditable(false);
        txtStrModifier.setText(org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.txtStrModifier.text")); // NOI18N
        txtStrModifier.setMinimumSize(new java.awt.Dimension(40, 20));
        txtStrModifier.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel1.add(txtStrModifier, new java.awt.GridBagConstraints());

        org.openide.awt.Mnemonics.setLocalizedText(lblCon, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.lblCon.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel1.add(lblCon, gridBagConstraints);

        spnConScore.setMinimumSize(new java.awt.Dimension(40, 20));
        spnConScore.setPreferredSize(new java.awt.Dimension(40, 20));
        spnConScore.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnConScoreMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel1.add(spnConScore, gridBagConstraints);

        spnConBonus.setMinimumSize(new java.awt.Dimension(40, 20));
        spnConBonus.setPreferredSize(new java.awt.Dimension(40, 20));
        spnConBonus.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnConBonusMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        jPanel1.add(spnConBonus, gridBagConstraints);

        txtConModifier.setEditable(false);
        txtConModifier.setText(org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.txtConModifier.text")); // NOI18N
        txtConModifier.setMinimumSize(new java.awt.Dimension(40, 20));
        txtConModifier.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        jPanel1.add(txtConModifier, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(lblDex, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.lblDex.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel1.add(lblDex, gridBagConstraints);

        spnDexScore.setMinimumSize(new java.awt.Dimension(40, 20));
        spnDexScore.setPreferredSize(new java.awt.Dimension(40, 20));
        spnDexScore.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnDexScoreMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        jPanel1.add(spnDexScore, gridBagConstraints);

        spnDexBonus.setMinimumSize(new java.awt.Dimension(40, 20));
        spnDexBonus.setPreferredSize(new java.awt.Dimension(40, 20));
        spnDexBonus.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnDexBonusMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        jPanel1.add(spnDexBonus, gridBagConstraints);

        txtDexModifier.setEditable(false);
        txtDexModifier.setText(org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.txtDexModifier.text")); // NOI18N
        txtDexModifier.setMinimumSize(new java.awt.Dimension(40, 20));
        txtDexModifier.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        jPanel1.add(txtDexModifier, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(lblInt, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.lblInt.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        jPanel1.add(lblInt, gridBagConstraints);

        spnIntScore.setMinimumSize(new java.awt.Dimension(40, 20));
        spnIntScore.setPreferredSize(new java.awt.Dimension(40, 20));
        spnIntScore.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnIntScoreMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        jPanel1.add(spnIntScore, gridBagConstraints);

        spnIntBonus.setMinimumSize(new java.awt.Dimension(40, 20));
        spnIntBonus.setPreferredSize(new java.awt.Dimension(40, 20));
        spnIntBonus.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnIntBonusMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        jPanel1.add(spnIntBonus, gridBagConstraints);

        txtIntModifier.setEditable(false);
        txtIntModifier.setText(org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.txtIntModifier.text")); // NOI18N
        txtIntModifier.setMinimumSize(new java.awt.Dimension(40, 20));
        txtIntModifier.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        jPanel1.add(txtIntModifier, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(lblWis, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.lblWis.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        jPanel1.add(lblWis, gridBagConstraints);

        spnWisScore.setMinimumSize(new java.awt.Dimension(40, 20));
        spnWisScore.setPreferredSize(new java.awt.Dimension(40, 20));
        spnWisScore.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnWisScoreMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        jPanel1.add(spnWisScore, gridBagConstraints);

        spnWisBonus.setMinimumSize(new java.awt.Dimension(40, 20));
        spnWisBonus.setPreferredSize(new java.awt.Dimension(40, 20));
        spnWisBonus.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnWisBonusMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        jPanel1.add(spnWisBonus, gridBagConstraints);

        txtWisModifier.setEditable(false);
        txtWisModifier.setText(org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.txtWisModifier.text")); // NOI18N
        txtWisModifier.setMinimumSize(new java.awt.Dimension(40, 20));
        txtWisModifier.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        jPanel1.add(txtWisModifier, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(lblCha, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.lblCha.text")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        jPanel1.add(lblCha, gridBagConstraints);

        spnChaScore.setMinimumSize(new java.awt.Dimension(40, 20));
        spnChaScore.setPreferredSize(new java.awt.Dimension(40, 20));
        spnChaScore.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnChaScoreMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        jPanel1.add(spnChaScore, gridBagConstraints);

        spnChaBonus.setMinimumSize(new java.awt.Dimension(40, 20));
        spnChaBonus.setPreferredSize(new java.awt.Dimension(40, 20));
        spnChaBonus.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                spnChaBonusMouseWheelMoved(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        jPanel1.add(spnChaBonus, gridBagConstraints);

        txtChaModifier.setEditable(false);
        txtChaModifier.setText(org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.txtChaModifier.text")); // NOI18N
        txtChaModifier.setMinimumSize(new java.awt.Dimension(40, 20));
        txtChaModifier.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        jPanel1.add(txtChaModifier, gridBagConstraints);

        prgsExperience.setMaximum(10);
        prgsExperience.setValue(5);
        prgsExperience.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        prgsExperience.setFocusable(false);
        prgsExperience.addMouseWheelListener(new java.awt.event.MouseWheelListener() {
            public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
                prgsExperienceMouseWheelMoved(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.jPanel3.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel7, org.openide.util.NbBundle.getMessage(ToonSandboxTopComponent.class, "ToonSandboxTopComponent.jLabel7.text")); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(47, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addContainerGap())))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(prgsExperience, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(prgsExperience, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void spnStrScoreMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnStrScoreMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityScore(evt.getWheelRotation(), t.getAbilities().get(AbilityType.STR), spnStrScore);
        updateModifier(t.getAbilities().get(AbilityType.STR), txtStrModifier);
    }//GEN-LAST:event_spnStrScoreMouseWheelMoved

    private void spnConScoreMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnConScoreMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityScore(evt.getWheelRotation(), t.getAbilities().get(AbilityType.CON), spnConScore);
        updateModifier(t.getAbilities().get(AbilityType.CON), txtConModifier);
    }//GEN-LAST:event_spnConScoreMouseWheelMoved

    private void spnDexScoreMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnDexScoreMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityScore(evt.getWheelRotation(), t.getAbilities().get(AbilityType.DEX), spnDexScore);
        updateModifier(t.getAbilities().get(AbilityType.DEX), txtDexModifier);
    }//GEN-LAST:event_spnDexScoreMouseWheelMoved

    private void spnIntScoreMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnIntScoreMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityScore(evt.getWheelRotation(), t.getAbilities().get(AbilityType.INT), spnIntScore);
        updateModifier(t.getAbilities().get(AbilityType.INT), txtIntModifier);
    }//GEN-LAST:event_spnIntScoreMouseWheelMoved

    private void spnWisScoreMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnWisScoreMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityScore(evt.getWheelRotation(), t.getAbilities().get(AbilityType.WIS), spnWisScore);
        updateModifier(t.getAbilities().get(AbilityType.WIS), txtWisModifier);
    }//GEN-LAST:event_spnWisScoreMouseWheelMoved

    private void spnChaScoreMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnChaScoreMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityScore(evt.getWheelRotation(), t.getAbilities().get(AbilityType.CHA), spnChaScore);
        updateModifier(t.getAbilities().get(AbilityType.CHA), txtChaModifier);
    }//GEN-LAST:event_spnChaScoreMouseWheelMoved

    private void spnStrBonusMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnStrBonusMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityMisc(evt.getWheelRotation(), t.getAbilities().get(AbilityType.STR), spnStrBonus);
        updateModifier(t.getAbilities().get(AbilityType.STR), txtStrModifier);
    }//GEN-LAST:event_spnStrBonusMouseWheelMoved

    private void spnConBonusMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnConBonusMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityMisc(evt.getWheelRotation(), t.getAbilities().get(AbilityType.CON), spnConBonus);
        updateModifier(t.getAbilities().get(AbilityType.CON), txtConModifier);
    }//GEN-LAST:event_spnConBonusMouseWheelMoved

    private void spnDexBonusMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnDexBonusMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityMisc(evt.getWheelRotation(), t.getAbilities().get(AbilityType.DEX), spnDexBonus);
        updateModifier(t.getAbilities().get(AbilityType.DEX), txtDexModifier);
    }//GEN-LAST:event_spnDexBonusMouseWheelMoved

    private void spnIntBonusMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnIntBonusMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityMisc(evt.getWheelRotation(), t.getAbilities().get(AbilityType.INT), spnIntBonus);
        updateModifier(t.getAbilities().get(AbilityType.INT), txtIntModifier);
    }//GEN-LAST:event_spnIntBonusMouseWheelMoved

    private void spnWisBonusMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnWisBonusMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityMisc(evt.getWheelRotation(), t.getAbilities().get(AbilityType.WIS), spnWisBonus);
        updateModifier(t.getAbilities().get(AbilityType.WIS), txtWisModifier);
    }//GEN-LAST:event_spnWisBonusMouseWheelMoved

    private void spnChaBonusMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_spnChaBonusMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        jLabel8.setText(t.getName());
        updateAbilityMisc(evt.getWheelRotation(), t.getAbilities().get(AbilityType.CHA), spnChaBonus);
        updateModifier(t.getAbilities().get(AbilityType.CHA), txtChaModifier);
    }//GEN-LAST:event_spnChaBonusMouseWheelMoved

    private void prgsExperienceMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN-FIRST:event_prgsExperienceMouseWheelMoved
        Collection<Toon> toons = (Collection<Toon>) result.allInstances();
        Iterator<Toon> it = toons.iterator();
        Toon t = it.next();
        Integer before = t.getExperience();
        Integer after = before + evt.getWheelRotation();
        String msg = "";
        if (after>=10) {
            after-=10;
            toonLevel.setValue(t, t.getLevel()+1);
//            t.gainLevel();
            msg = "Level Gained";
        }else if (after<0) {
            after+=10;
            toonLevel.setValue(t, t.getLevel()-1);
//            t.loseLevel();
            msg = "Level Lost!";
        }
        t.setExperience(after);
        jLabel8.setText(msg+":"+t.getExperience());
        repaint();
    }//GEN-LAST:event_prgsExperienceMouseWheelMoved

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblCha;
    private javax.swing.JLabel lblCon;
    private javax.swing.JLabel lblDex;
    private javax.swing.JLabel lblInt;
    private javax.swing.JLabel lblStr;
    private javax.swing.JLabel lblWis;
    private javax.swing.JProgressBar prgsExperience;
    private javax.swing.JSpinner spnChaBonus;
    private javax.swing.JSpinner spnChaScore;
    private javax.swing.JSpinner spnConBonus;
    private javax.swing.JSpinner spnConScore;
    private javax.swing.JSpinner spnDexBonus;
    private javax.swing.JSpinner spnDexScore;
    private javax.swing.JSpinner spnIntBonus;
    private javax.swing.JSpinner spnIntScore;
    private javax.swing.JSpinner spnStrBonus;
    private javax.swing.JSpinner spnStrScore;
    private javax.swing.JSpinner spnWisBonus;
    private javax.swing.JSpinner spnWisScore;
    private javax.swing.JTextField txtChaModifier;
    private javax.swing.JTextField txtConModifier;
    private javax.swing.JTextField txtDexModifier;
    private javax.swing.JTextField txtIntModifier;
    private javax.swing.JTextField txtStrModifier;
    private javax.swing.JTextField txtWisModifier;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized ToonSandboxTopComponent getDefault() {
        if (instance == null) {
            instance = new ToonSandboxTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the ToonSandboxTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized ToonSandboxTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(ToonSandboxTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof ToonSandboxTopComponent) {
            return (ToonSandboxTopComponent) win;
        }
        Logger.getLogger(ToonSandboxTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        result = WindowManager.getDefault().findTopComponent("ToonListingTopComponent").getLookup().lookupResult(Toon.class);
        result.addLookupListener(this);
        resultChanged(new LookupEvent(result));
    }

    @Override
    public void componentClosed() {
        result.removeLookupListener(this);
        result = null;
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    BindingGroup bindingGroup;
    BeanProperty<Toon, Integer> level;
    Property<Toon, Integer> levelProperty;
    BeanProperty<Toon, Integer> toonLevel = BeanProperty.create(levelProperty, "level");

    @Override
    public void resultChanged(LookupEvent ev) {
        Lookup.Result r = (Lookup.Result) ev.getSource();
        Collection<Toon> toons = r.allInstances();
        if (!toons.isEmpty()) {
            for (Toon toon : toons) {
                bindingGroup = new BindingGroup();
		toonLevel.addPropertyStateListener(toon, new PropertyStateListener() {

                    @Override
                    public void propertyStateChanged(PropertyStateEvent pse) {
                        Toon toon = (Toon) pse.getSourceObject();
                        jLabel8.setText(pse.toString());
			LookupEvent event = new LookupEvent(WindowManager.getDefault().findTopComponent("ToonListingTopComponent").getLookup().lookupResult(Toon.class));
			Utilities.updateUI(event, SkillsTopComponent.findInstance(), DefensesTopComponent.findInstance());
                    }
                });
                jLabel7.setText(toon.getName());
                spnStrScore.setValue(toon.getAbilities().get(AbilityType.STR).getScore());
                spnConScore.setValue(toon.getAbilities().get(AbilityType.CON).getScore());
                spnDexScore.setValue(toon.getAbilities().get(AbilityType.DEX).getScore());
                spnIntScore.setValue(toon.getAbilities().get(AbilityType.INT).getScore());
                spnWisScore.setValue(toon.getAbilities().get(AbilityType.WIS).getScore());
                spnChaScore.setValue(toon.getAbilities().get(AbilityType.CHA).getScore());
                spnStrBonus.setValue(toon.getAbilities().get(AbilityType.STR).getMisc());
                spnConBonus.setValue(toon.getAbilities().get(AbilityType.CON).getMisc());
                spnDexBonus.setValue(toon.getAbilities().get(AbilityType.DEX).getMisc());
                spnIntBonus.setValue(toon.getAbilities().get(AbilityType.INT).getMisc());
                spnWisBonus.setValue(toon.getAbilities().get(AbilityType.WIS).getMisc());
                spnChaBonus.setValue(toon.getAbilities().get(AbilityType.CHA).getMisc());
                txtStrModifier.setText(toon.getAbilities().get(AbilityType.STR).getModifier()+"");
                txtConModifier.setText(toon.getAbilities().get(AbilityType.CON).getModifier()+"");
                txtDexModifier.setText(toon.getAbilities().get(AbilityType.DEX).getModifier()+"");
                txtIntModifier.setText(toon.getAbilities().get(AbilityType.INT).getModifier()+"");
                txtWisModifier.setText(toon.getAbilities().get(AbilityType.WIS).getModifier()+"");
                txtChaModifier.setText(toon.getAbilities().get(AbilityType.CHA).getModifier()+"");
                prgsExperience.setModel(new ExperienceProgressModel(toon));
            }
        }else{
            jLabel7.setText("[None Selected]");
            spnStrScore.setValue(0);
            spnStrBonus.setValue(0);
            spnConScore.setValue(0);
            spnConBonus.setValue(0);
            spnDexScore.setValue(0);
            spnDexBonus.setValue(0);
            spnIntScore.setValue(0);
            spnIntBonus.setValue(0);
            spnWisScore.setValue(0);
            spnWisBonus.setValue(0);
            spnChaScore.setValue(0);
            spnChaBonus.setValue(0);
            txtStrModifier.setText("");
            txtConModifier.setText("");
            txtDexModifier.setText("");
            txtIntModifier.setText("");
            txtWisModifier.setText("");
            txtChaModifier.setText("");
            prgsExperience.setMinimum(0);
            prgsExperience.setMaximum(1);
            prgsExperience.setValue(0);
        }
    }
}
